package com.example.nyf.mapper.provider;

import com.example.nyf.dto.SysUserTDO;

import java.util.Map;

public class SysUserProvider {

    /**
     * 查询用户多条件
     * @param mp
     * @return
     */
    public String getSysUserList(Map<String, Object> mp) {
        StringBuffer sb = new StringBuffer("select a.*, GROUP_CONCAT(b.role_id) as role, GROUP_CONCAT(c.role_name) as role_name from sys_user a left outer join sys_user_role b on a.id=b.user_id " +
                " left outer join sys_role c on b.role_id=c.id where 1=1  ");

        if(mp.get("user_name")!=null && !("").equals(mp.get("user_name"))) {
            sb.append(" and a.user_name like '%"+mp.get("user_name")+"%'");
        }
        if(mp.get("status")!=null && !("").equals(mp.get("status"))) {
            sb.append(" and a.status ="+mp.get("status")+"");
        }
        if(mp.get("roleId")!=null && !("").equals(mp.get("roleId"))) {
            sb.append(" and b.role_id ="+mp.get("roleId")+"");
        }
        sb.append(" group by a.id  ");
        return sb.toString();
    }

    public String updateSysUser(SysUserTDO sysUserTDO) {
        StringBuffer sb = new StringBuffer("update sys_user set status = "+sysUserTDO.getStatus()+", remark = '"+sysUserTDO.getRemark()+"' ");
        if(sysUserTDO.getPassword()!=null && !"".equals(sysUserTDO.getPassword())) {
            sb.append(" , password = '"+sysUserTDO.getPassword()+"' ");
        }
        if(sysUserTDO.getAvatar()!=null && !"".equals(sysUserTDO.getAvatar())) {
            sb.append(" , avatar = '"+sysUserTDO.getAvatar()+"' ");
        }
        sb.append(" where id = "+sysUserTDO.getId()+"");
        return sb.toString();
    }
}
